import Vue from 'vue'
import VueRouter from 'vue-router'
// 分组懒加载 /* webpackChunkName:"login_home_welcome" */
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/login')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/home')
const welcom = () => import(/* webpackChunkName:"login_home_welcome"*/ '../components/welcom')
const users = () => import(/* webpackChunkName:"users_rights_roles" */ '../components/user/users')
const rights = () => import(/* webpackChunkName:"users_rights_roles" */ '../components/power/rights')
const roles = () => import(/* webpackChunkName:"users_rights_roles" */ '../components/power/roles')
const categories = () => import(/* webpackChunkName:"categories_params"*/ '../components/goods/cate')
const params = () => import(/* webpackChunkName:"categories_params"*/ '../components/goods/params')
const goods = () => import(/* webpackChunkName:"goods_addProduct"*/ '../components/goods/goods')
const addProduct = () => import(/* webpackChunkName:"goods_addProduct" */ '../components/goods/addProduct')
const order = () => import(/* webpackChunkName:"order_report" */ '../components/order/order')
const report = () => import(/* webpackChunkName:"order_report" */ '../components/report/report')

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/Login'
    },
    {
      path: '/Login',
      name: 'Login',
      component: Login
    },
    {
      path: '/Home',
      name: 'Home',
      component: Home,
      redirect: '/welcom',
      children: [
        {
          path: '/welcom',
          name: 'welcom',
          component: welcom,
        },
        {
          path: '/users',
          name: 'users',
          component: users,
        },
        {
          path: '/rights',
          name: 'rights',
          component: rights,
        },
        {
          path: '/roles',
          name: 'roles',
          component: roles,
        },
        {
          path: '/categories',
          name: 'categories',
          component: categories,
        },
        {
          path: '/params',
          name: 'params',
          component: params,
        },
        {
          path: '/goods',
          name: 'goods',
          component: goods,
        },
        {
          path: '/goods/addProduct',
          name: 'addProduct',
          component: addProduct,
        },
        {
          path: '/orders',
          name: 'order',
          component: order,
        },
        {
          path: '/reports',
          name: 'report',
          component: report,
        }
      ]
    }
  ]
})

// 挂载路由守卫
router.beforeEach((to, from, next)=>{
  // 登录页面，进入直接放行
  if(to.path === '/Login') return next()
  // 从sessionStorage中读取已经保存的token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/Login')
  next()
} )

export default router
